package com.cmw.auntapp.ui;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.cmw.auntapp.R;
import com.cmw.auntapp.constant.CommandType;
import com.cmw.auntapp.core.base.vo.DataResult;
import com.cmw.auntapp.core.util.FormUtil;
import com.cmw.auntapp.core.util.SensitiveInfoUtils;
import com.cmw.auntapp.core.util.StringHandler;
import com.cmw.auntapp.presenter.AuntPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.OnClick;

import static com.cmw.auntapp.R2.id.iv_my;
import static com.cmw.auntapp.R2.id.tv_val_address;
import static com.cmw.auntapp.R2.id.tv_val_age;
import static com.cmw.auntapp.R2.id.tv_val_card_no;
import static com.cmw.auntapp.R2.id.tv_val_place;
import static com.cmw.auntapp.R2.id.tv_val_service_type;
import static com.cmw.auntapp.R2.id.tv_val_sex;
import static com.cmw.auntapp.constant.Constant.SHOW_LOADING_KEY;
import static com.cmw.auntapp.constant.Constant.SHOW_LOADING_YES;

/**
 *  用户帐户详情
 *  @auth chengmingwei
 *  @date 2021/08/06 07:17
 *  @contanct 13922111489/340360491@qq.com
 */
public class UserDetailActivity extends BaseActivity {

    AuntPresenter auntPresenter;

    @Override
    void onInit(Bundle savedInstanceState) {
        setTitle(R.string.user_center_title);
        auntPresenter = new AuntPresenter(this);

    }

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_user_detail;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume...");
        loadData();
    }

    private void loadData() {
        Map<String, String> params = new HashMap<>();
        params.put(SHOW_LOADING_KEY, SHOW_LOADING_YES);
        params.put("memberInfo","1"); // 加载会员信息
        loading();
        auntPresenter.get(params); // 获取阿姨详细信息数据
    }

    @OnClick({R.id.iv_my, R.id.tv_val_name,tv_val_sex, tv_val_age, tv_val_place,
            tv_val_service_type, tv_val_card_no, tv_val_address})
    public void bindClickEvent(View view) {
        int viewId = view.getId();
        if (viewId == iv_my) { // 上传用户图像
            return;
        }
        go(UserEditActivity.class);
    }

    @Override
    protected boolean doSuccess(CommandType commandType, DataResult dataResult) {
        switch (commandType) {
            case GET:{ // 阿姨详情数据
                setVals(dataResult);
                break;
            } default: {
                Log.e(TAG, StringHandler.formatStr("错误的 commandType = %s", commandType.name()));
            }
        }
        return true;
    }

    /**
     * 页面详情数据赋值
     * @param dataResult
     */
    private void setVals(DataResult dataResult) {
        int[] viewIds = {R.id.iv_my, R.id.tv_val_name,tv_val_sex, tv_val_age, tv_val_place,
                tv_val_service_type, tv_val_card_no, tv_val_address};
        JSONObject formData = (JSONObject) dataResult.getDatas();
        Integer serviceTypes = formData.getInteger("serviceTypes");
        if (null == serviceTypes) {
            serviceTypes = 1; // 钟点工
        }
        String serviceTypeName = "钟点工";
        switch (serviceTypes.intValue()) {
            case 1 : { // 钟点工
                serviceTypeName = "钟点工";
                break;
            } case 2 : { // 跑腿
                serviceTypeName = "跑腿";
                break;
            } case 3 : { // 月嫂
                serviceTypeName = "月嫂";
                break;
            }
        }
        formData.put("serviceTypeName", serviceTypeName);
        Integer sex = formData.getInteger("sex");
        String sexName = "暂未设置";
        if (null != sex) {
            switch (sex.intValue()) {
                case 1 : { // 男
                    sexName = "男";
                    break;
                } case 2 : { // 女
                    sexName = "女";
                    break;
                }
            }
        }
        formData.put("sexName", sexName);
        String idcardNo = formData.getString("idcardNo");
        if (StringHandler.isValidStr(idcardNo)) {
            idcardNo = SensitiveInfoUtils.idCard(idcardNo);
            formData.put("idcardNo", idcardNo);
        }
        String address = formData.getString("address");
        if (StringHandler.isValidStr(address)) {
            address = SensitiveInfoUtils.address(address, 4);
            formData.put("address", address);
        }
        Log.i(TAG, formData.toJSONString());
        FormUtil.setValues(this, formData, viewIds);
    }

    @Override
    protected boolean doFailure(CommandType commandType, DataResult dataResult) {
        return true;
    }

}